package com.glorysoft.xztpda.ui.datacollection

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.glorysoft.xztpda.entity.ui.ToastMessageBean
import com.glorysoft.xztpda.BR
import com.glorysoft.xztpda.R
import com.glorysoft.xztpda.base.BaseActivity
import com.glorysoft.xztpda.databinding.ActivityDataCollectionBinding
import com.glorysoft.xztpda.entity.EdcDataEDCSETLISTEDCSETBINLINELIST
import com.huawei.hms.hmsscankit.ScanUtil
import com.huawei.hms.ml.scan.HmsScan
import kotlinx.android.synthetic.main.activity_data_collection.*
import kotlinx.android.synthetic.main.activity_data_collection.etLotID
import kotlinx.android.synthetic.main.activity_data_collection.ivLotScan
import kotlinx.android.synthetic.main.activity_track_in_guide.*

/**
 * FileName: CollectionDataActivity
 * Author: XJH
 * Date: 2022/4/13 16:29
 * Description:
 * History:
 */
class CollectionDataActivity : BaseActivity<CollectionDataViewModel,ActivityDataCollectionBinding>() {

    var tempItem  : Int = 0

    override fun layoutId(): Int {
        return R.layout.activity_data_collection
    }

    override fun initVariableId(): Int {
        return BR.collectionDataViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle("数据收集")
        setRightListener("提交"){
            if (viewModel.lotInfoEntity == null){
                viewModel.defUI.toastEvent.value = ToastMessageBean("未获取到批次信息",false)
                return@setRightListener
            }

            if (viewModel.dataAdapter.data.isEmpty()){
                viewModel.defUI.toastEvent.value = ToastMessageBean("未获取到BIN信息",false)
                return@setRightListener
            }

            viewModel.dataAdapter.data.forEach {
                try {
                    if (it.VALUE.toInt() < 0){
                        viewModel.defUI.toastEvent.value = ToastMessageBean("数量不能为负数",false)
                        return@setRightListener
                    }
                }catch (e : Exception){

                }

            }


            viewModel.submit()
        }

        rvData.adapter = viewModel.dataAdapter
        ivLotScan.setOnClickListener{startScanForCode(this,10001)}
        tvConfirm.setOnClickListener{
            if (tvBinName.text.toString().isEmpty()){
                viewModel.defUI.toastEvent.value = ToastMessageBean("请选择BIN信息",false)
            }else{
                try {
                    val toInt = etBinNum.text.toString().toInt()
                    val toInt1 = etBinNum2.text.toString().toInt()
                    val toInt2 = viewModel.edcData?.ENDLOTQTY
                    if ((toInt + toInt1 - toInt2!!) < 0){
                        viewModel.defUI.toastEvent.value = ToastMessageBean("数量输入有误",false)
                        return@setOnClickListener
                    }
                    viewModel.dataAdapter.data[tempItem].VALUE = (toInt + toInt1 - toInt2).toString()
                }catch (e : Exception){
                    viewModel.dataAdapter.data[tempItem].VALUE = "0"
                }

                viewModel.dataAdapter.notifyItemChanged(tempItem)
                etBinNum.setText("")
                etBinNum2.setText("")
                tvBinName.text = ""
            }
        }
    }

    override fun initData() {
        initEdit(etLotID,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.getLotInfo(s)
            }

        })

        viewModel.defUI.callObserve.observe(this){
            if (it.type == 1001){
                tempItem = it.item as Int
                tvBinName.text = viewModel.dataAdapter.data[tempItem].NAME
            }else{
                mBinding.setVariable(BR.collectionDataViewModel,viewModel)
                tvBinName.text = ""
            }

        }

        viewModel.defUI.startActivity.observe(this){
            viewModel.defUI.toastEvent.value = ToastMessageBean("操作成功",true)
            finish()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

        // 当扫码页面结束后，处理扫码结果。
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != RESULT_OK || data == null) {
            return
        }
        if (resultCode == RESULT_OK){
            // 从onActivityResult返回data中，用ScanUtil.RESULT作为key值取到HmsScan返回值。
            when (val obj: Any = data.getParcelableExtra(ScanUtil.RESULT)) {
                is HmsScan -> {
                    val barCode = obj.getOriginalValue()
                    when(requestCode){
                        10001 -> {
                            etLotID.setText(barCode)
                            viewModel.getLotInfo(barCode)
                        }
                    }
                }
            }


        }
    }
}